package Game.Objects;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import GUI.GuiManager;
import GUI.GuiMessenger;
import Game.Level;
import Screens.MyScreens.GameScreen;
import Tools.SoundManager;
import Tools.Drawing.LayersHelper;

/** Lever */
public class GOLever extends GameObject {
	
	/** Is on? */
	private boolean _isOn = false;
	
	public GOLever(ObjectManager objectManager) throws SlickException {
		super(objectManager, "Lever");
		
		_isCanBeUsed = true;
		
		setSprite(new Image("res/gfx/objects/lever_off.png"));
		setLayerForDrawing(LayersHelper.LAYER_OBJECTS);
	}

	@Override
	public void use(GameObject user) throws SlickException {
		if (user.isPlayer()) {
			// Show message
			GuiManager.getMessenger().addMessage("[green]You[/green] switched a [yellow]"
					+ this.getName() + "[/yellow]");

			if (_isOn) {
				_isOn = false;

				setSprite(new Image("res/gfx/objects/lever_off.png"));
				
				// Close trapdoor
				GameObject[] gameObjects = _objectManager.getObjectsByClass("Game.Objects.GOTrapdoor");
				((GOTrapdoor)gameObjects[0]).close();
			} else {
				_isOn = true;

				setSprite(new Image("res/gfx/objects/lever_on.png"));
				
				// Open trapdoor
				GameObject[] gameObjects = _objectManager.getObjectsByName("Trapdoor");
				((GOTrapdoor)gameObjects[0]).open();
			}
			
			// Play sound
			SoundManager.play("sfx_lever");
		}
	}

}
